// components/dinnerHeader/dinnerHeader.js
import {
  list
} from '../../api/ad'
const app = getApp()
const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    adPositionId: {
      type: String
    },
    mediaType: {
      type: String,
      value: 'img',
    }
  },
  lifetimes: {
    attached() {
      this.loadAd(this.data.adPositionId)
      this.data.address = app.globalData.address
      this.setData({
        address: this.data.address
      })
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    address: '',
    adForm: {},
    id: {},
    swipersIndex: 0,
    swiperVideoIndex: 0,
    // array: [1, 2, 3, 4, 5]
  },
  /**
   * 组件的方法列表
   */
  methods: {
    changeVideoIndex() {
      this.setData({
        swiperVideoIndex: (this.data.swiperVideoIndex + 1) % this.data.adForm.length
      })
    },
    bindChange(e) {
      let {
        current,
        source
      } = e.detail
      if (source === 'autoplay' || source === 'touch') {
        this.setData({
          swipersIndex: current
        })
      }
    },
    loadAd(id) {
      list({
        adPositionId: id,
        pageSize: 40
      }).then(res => {
        this.data.adForm = res.data.data
        this.data.adForm = this.data.adForm.map(e => {
          if(this.data.mediaType=='video'){
            return {
              ...e,
              thumbnail: util.thumbnail(e.thumbnail, 710,392),
              url:e.thumbnail.replace(
                /^(.*?)([\w-]{20,})(.*?)$/,
                app.APP_OSSURL_API+'/dinnerBar/faxian/lunbo/$2.mp4'
                ),
            }
          }else{
            return {
              ...e,
              thumbnail: util.thumbnail(e.thumbnail, 710, 200)
            }
          }
          
        })
        console.log("forVideoSwipper: ",this.data.adForm)
        this.setData({
          adForm: this.data.adForm
        })
      })
    },
    changeAddress() {
      wx.navigateTo({
        url: '/pages/member/address/address?type=change',
        success: function (res) {},
        fail: function (res) {},
        complete: function (res) {},
      })
    },
    toDetail(e) {

      if (e.currentTarget.dataset.type == 7) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + e.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (e.currentTarget.dataset.type == 5) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + e.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (e.currentTarget.dataset.type == 4) {
        wx.navigateTo({
          url: '/pages/travel/travel/index?scenicSpotId=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 6) {
        wx.navigateTo({
          url: '/pages/cook/kitchenBrigade/kitchenBrigade?id=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 2) {
        wx.navigateTo({
          url: '/pages/hotspot/hotspot/index?id=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 3) {
        wx.navigateTo({
          url: '/pages/discovery/details/details?id=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 0) {
        wx.navigateTo({
          url: '/pages/shop/productDetail/productDetail?productId=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 9) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id=' + e.currentTarget.dataset.id
        })
      } else if (e.currentTarget.dataset.type == 8) {
        wx.navigateTo({
          url: '/pages/member/coupon/index',
        })
      }

    },
    searchClick() {
      wx.navigateTo({
        url: '/pages/goods/search/index'
      })
    },
  }
})